<?php
class ContentTypeFieldsModel {
	public static function get ($config) {
		return SleekPHP::db()->get('content_type_fields', $config);
	}

	public static function insert ($row) {
		return self::insertUpdate($row);
	}

	public static function update ($id, $row) {
		return self::insertUpdate($row, $id);
	}

	private static function insertUpdate ($row, $id = false) {
		$content_types_id	= isset($row['content_types_id']) ? $row['content_types_id'] : 0;
		$validation			= isset($row['validation']) ? $row['validation'] : '/^.+$/';
		$title				= isset($row['title']) ? $row['title'] : 'Untitled';
		$name				= isset($row['name']) ? $row['name'] : str_replace('_', '-', urlize($title));

		$fields = array(
			'content_types_id'	=> $content_types_id, 
			'validation'		=> $validation, 
			'title'				=> $title, 
			'name'				=> $name
		);

		if ($id) {
			return SleekPHP::db()->update('content_type_fields', $id, $fields);
		}
		else {
			return SleekPHP::db()->insert('content_type_fields', $fields);
		}
	}

	public static function delete ($id) {
		return SleekPHP::db()->delete('content_type_fields', $id);
	}
}
